<template>
  <el-card>
    <div slot="header" class="clearfix relative">
      <span class="title-icon">基本信息</span>
      <el-button
        v-if="isEdit"
        type="success"
        size="mini"
        class="header-save-btn"
        @click="changeBasicInfo"
      >
        保存
      </el-button>
    </div>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
      size="mini"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item prop="returnCountry">
            <div slot="label">退货国家:</div>
            <el-select
              v-model="ruleForm.returnCountry"
              filterable
              placeholder="请选择退货国家"
            >
              <el-option
                v-for="item in $store.state.public.publicData.countryList"
                :key="item.value"
                :label="item.label"
                :value="parseInt(item.value)"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item prop="shippingWay">
        <div slot="label">业务类型:</div>
        <el-radio-group v-model="ruleForm.shippingWay">
          <el-radio label="TH">退货</el-radio>
          <el-radio label="HWC">中转</el-radio>
          <el-radio label="OT">其他</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="serviceItemArr">
        <div slot="label">服务项目:</div>

        <el-checkbox-group v-model="ruleForm.serviceItemArr">
          <el-checkbox
            v-for="(item, value) in $store.state.public.publicData
              .serviceItemMap"
            :key="value"
            :label="value"
            name="specialProduct"
          >
            {{ item }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item prop="customerRemark">
        <div slot="label">运单备注:</div>

        <el-input
          v-model="ruleForm.customerRemark"
          type="textarea"
          style="width:60%"
        />
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { saveAbroadWaybill } from '@/api/abroadWaybill'
export default {
  name: 'BasicInfo',
  components: {},
  props: {
    countryList: {
      type: Array,
      default: function(params) {
        return []
      }
    },
    serviceItemMap: {
      type: Object,
      default: function(params) {
        return {}
      }
    },
    waybillInfo: {
      type: Object,
      default: function(params) {
        return {}
      }
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      waybillId: '',
      ruleForm: {
        returnCountry: '',
        serviceItemArr: [],
        serviceItem: '',
        shippingWay: '',
        customerRemark: ''
      },
      rules: {
        returnCountry: [
          { required: true, message: '请选择退货国家', trigger: 'change' }
        ],
        serviceItemArr: [
          {
            type: 'array',
            required: true,
            message: '请至少选择一个服务项目',
            trigger: 'change'
          }
        ],
        shippingWay: [
          { required: true, message: '请选择业务类型', trigger: 'change' }
        ],
        customerRemark: [
          { required: false, message: '请填写活动形式', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    waybillInfo(newValue, oldValue) {
      this.ruleForm.returnCountry = newValue.returnCountry
      this.ruleForm.serviceItem = newValue.serviceItem
      this.ruleForm.serviceItemArr = this.ruleForm.serviceItem.split(',')
      this.ruleForm.shippingWay = newValue.shippingWay
      this.ruleForm.customerRemark = newValue.customerRemark
      this.ruleForm.id = newValue.id
    }
  },
  mounted() {},
  methods: {
    changeBasicInfo() {
      this.$confirm('确认保存修改？')
        .then(_ => {
          this.saveInfo()
        })
        .catch(_ => {})
    },
    async saveInfo() {
      this.$store.dispatch('app/toggleDialog', true)
      // console.log('this.ruleForm :>> ', this.ruleForm)
      this.ruleForm.serviceItem = this.ruleForm.serviceItemArr.join(',')
      // return
      const { code, message } = await saveAbroadWaybill(this.ruleForm)

      if (code === 200) {
        this.$message({
          type: 'success',
          message: '保存成功'
        })
      } else {
        this.$message.error(message)
      }
      this.$store.dispatch('app/toggleDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.relative {
  .absolute {
    right: 10px;
    top: -40%;
  }
}
</style>
